//
//  main.swift
//  7.Optional Chaining
//
//  Created by 左忠飞 on 2021/8/11.
//

import Foundation

class Toy{
    let name:String
    init(name:String){
        self.name = name
    }
}

class Pet {
    var toy:Toy?
}

class Child {
    var pet:Pet?
}

//想要知道小孩的宠物的玩具名字,可以通过Optional Chaining拿到
let xiaoming = Child()

let toyName = xiaoming.pet?.toy?.name
print(toyName ?? "没有玩具")

if let toyName = xiaoming.pet?.toy?.name {
    print("玩具名:\(toyName)")
}

extension Toy {
    func play() {
        print("带宠物玩玩具")
    }
}


let playClosure = {(child:Child) -> ()? in
    child.pet?.toy?.play()
}

playClosure(xiaoming)

if let result: () = playClosure(xiaoming) {
    print("好开心")
}else{
    print("没有玩具可以玩")
}


